package org.noworks.game;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Main extends StateBasedGame {

	public static final int MAIN_MENU_STATE = 0;
	public static final int GAMEPLAY_STATE = 1;
	public static final int OPTIONS_STATE = 2;
	public static final int CHAPTER_STATE = 3;

	public static int width = 1366;
	public static int height = width*9/16;
	
	public static int world = 1;
	public static int level = 1;

	public static final float scale = width / 1920f;

	public static void main(String[] args) throws SlickException {
		initSettings();
		AppGameContainer app = new AppGameContainer(new Main());
		app.setDisplayMode(width, height, false);
		app.start();
	}

	public Main() {
		super("Platformer");
		// TODO Auto-generated constructor stub
		this.addState(new MainMenu(MAIN_MENU_STATE));
		this.addState(new Gameplay(GAMEPLAY_STATE));
		this.enterState(MAIN_MENU_STATE);
	}

	@Override
	public void initStatesList(GameContainer gc) throws SlickException {
		// TODO Auto-generated method stub
		this.getState(MAIN_MENU_STATE).init(gc, this);
		this.getState(GAMEPLAY_STATE).init(gc, this);
		//this.getState(OPTIONS_STATE).init(gc, this);
		//this.getState(CHAPTER_STATE).init(gc, this);
	}
	
	public static void initSettings() {
		Scanner in = null;
		try {
			in = new Scanner(new File("data/settings"));
		} catch (FileNotFoundException e) {
			width = 1366;
			height = 768;
			world = 1;
			level = 1;
			printSettings();
		}
		in.useDelimiter("\n");
		while (in.hasNext()) {
			String[] temp = in.next().split("=");
			if (temp[0].equalsIgnoreCase("width"))
				width = Integer.parseInt(temp[1]);
			if (temp[0].equalsIgnoreCase("height"))
				height = Integer.parseInt(temp[1]);
			if (temp[0].equalsIgnoreCase("world"))
				world = Integer.parseInt(temp[1]);
			if (temp[0].equalsIgnoreCase("level"))
				level = Integer.parseInt(temp[1]);
		}
		in.close();
	}
	
	public static void printSettings() {
		PrintWriter out = null;
		try {
			out = new PrintWriter(new File("data/settings"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.println(width);
		out.println(height);
		out.println(world);
		out.print(level);
		out.close();
	}

}
